package org.soarsky.wealthchengdu.module.set;

import org.soarsky.wealthchengdu.R;
import org.soarsky.wealthchengdu.appliction.MyAppliction;
import org.soarsky.wealthchengdu.db.DBtool;
import org.soarsky.wealthchengdu.entity.NewsContentData;
import org.soarsky.wealthchengdu.language.LanguageUtil;
import org.soarsky.wealthchengdu.module.focus.FocusActivity;
import org.soarsky.wealthchengdu.network.ParserResults;
import org.soarsky.wealthchengdu.util.Config;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class CollectActivity extends Activity {
	private LanguageUtil util;
	ImageView titleReturn;
	TextView titleName;
	ListView collectListView;
	private CollectListAdapter adapter;// list适配器
	private ParserResults service;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_collect);
		service = new ParserResults();
		intnView();
		getcuser();
		adapter = new CollectListAdapter(CollectActivity.this);
		collectListView.setAdapter(adapter);
		titleReturn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				CollectActivity.this.finish();
			}
		});
		collectListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				MyAppliction.ifcollect = true;
				Intent intent = new Intent(CollectActivity.this,
						FocusActivity.class);
				Bundle bundle = new Bundle();
				bundle.putInt("position", position);
				intent.putExtras(bundle);
				startActivity(intent);

			}
		});
		adapter.setlistdata(MyAppliction.collectlistID);
	}

	public void intnView() {
		util = Config.changeLanguage(MyAppliction.mCn);
		
		titleReturn = (ImageView) findViewById(R.id.title_iv_img);
		titleName = (TextView) findViewById(R.id.title_tv_text);
		titleName.setText(util.getFavorite());
		collectListView = (ListView) findViewById(R.id.activity_collect_listview);
	}

	/**
	 * 获取收藏数据
	 */
	public void getcuser() {
		if (MyAppliction.collectlistID.size() > 0) {
			MyAppliction.collectlistID.clear();
		}
		Cursor cursor = DBtool.getDBTool(this).select();
		if (cursor.getCount() > 0) {

			for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor
					.moveToNext()) {
				NewsContentData newsContentData = new NewsContentData();
				newsContentData.setId(cursor.getString(cursor
						.getColumnIndex("idto")));
				newsContentData.setTitle(cursor.getString(cursor
						.getColumnIndex("title")));
				newsContentData.setTitleEn(cursor.getString(cursor
						.getColumnIndex("titleEn")));
				newsContentData.setSubTitle(cursor.getString(cursor
						.getColumnIndex("subTitle")));
				newsContentData.setSubTitleEn(cursor.getString(cursor
						.getColumnIndex("subTitleEn")));
				newsContentData.setImg(cursor.getString(cursor
						.getColumnIndex("img")));
				newsContentData.setImgEn(cursor.getString(cursor
						.getColumnIndex("imgEn")));
				newsContentData.setSource(cursor.getString(cursor
						.getColumnIndex("source")));
				newsContentData.setSourceEn(cursor.getString(cursor
						.getColumnIndex("sourceEn")));
				newsContentData.setContent(cursor.getString(cursor
						.getColumnIndex("content")));
				newsContentData.setContentEn(cursor.getString(cursor
						.getColumnIndex("contentEn")));

				newsContentData.setAdmireNum(cursor.getString(cursor
						.getColumnIndex("admireNum")));
				newsContentData.setRemarkNum(cursor.getString(cursor
						.getColumnIndex("remarkNum")));
				newsContentData.setShareNum(cursor.getString(cursor
						.getColumnIndex("shareNum")));
				newsContentData.setAddTime(cursor.getString(cursor
						.getColumnIndex("addTime")));
				newsContentData.setPvNum(cursor.getString(cursor
						.getColumnIndex("pvNum")));
				newsContentData.setCollectNum(cursor.getString(cursor
						.getColumnIndex("collectNum")));
				MyAppliction.collectlistID.add(newsContentData);
			}
		} else {
			Toast.makeText(getApplicationContext(), "没收藏文章...",
					Toast.LENGTH_LONG).show();
		}
		cursor.close();
	}

}
